package com.thirtydays.frame.ui.text

import android.content.Context
import android.graphics.Color
import android.os.CountDownTimer
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.thirtydays.frame.R
import com.thirtydays.frame.databinding.DownTimeItemViewBinding
import com.thirtydays.frame.util.ext.formatTimes
import com.thirtydays.frame.util.ext.gone


/**
 * @author 小垚
 * @创建时间： 2020/12/17
 * @描述：倒计时控件
 **/
class DownTimeItemTextView : FrameLayout {

    constructor(context: Context) : super(context) {
        initView(context, null, 0)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView(context, attrs, defStyleAttr)
    }

    lateinit var viewBinding: DownTimeItemViewBinding
    private var downTime: CountDownTimer? = null

    private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
        viewBinding = DownTimeItemViewBinding.inflate(LayoutInflater.from(context), this, true)

        val downTime = context.obtainStyledAttributes(attrs, R.styleable.DownTimeTextView, defStyleAttr, 0)
        val textSize = context.resources.getDimension(R.dimen.font_12)
        val downTextSize = downTime.getDimension(R.styleable.DownTimeTextView_downTextSize, textSize)
        val downHeight = downTime.getDimension(R.styleable.DownTimeTextView_downHeight, -1f)
        val downColor = downTime.getColor(R.styleable.DownTimeTextView_downTextColor, Color.parseColor("#F66262"))
        val downSymbolColor = downTime.getColor(R.styleable.DownTimeTextView_downSymbolTextColor, Color.parseColor("#F66262"))
        val downDrawable = downTime.getResourceId(R.styleable.DownTimeTextView_downDrawable, -1)

        var downPaddingLeft = downTime.getDimension(R.styleable.DownTimeTextView_downPaddingLeft, 0f).toInt()
        var downPaddingRight = downTime.getDimension(R.styleable.DownTimeTextView_downPaddingRight, 0f).toInt()
        var downPaddingTop = downTime.getDimension(R.styleable.DownTimeTextView_downPaddingTop, 0f).toInt()
        var downPaddingBottom = downTime.getDimension(R.styleable.DownTimeTextView_downPaddingBottom, 0f).toInt()
        var downPadding = downTime.getDimension(R.styleable.DownTimeTextView_downPadding, 0f).toInt()


        viewBinding.tvDayItem.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)
        viewBinding.tvDaySymbol.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)
        viewBinding.tvHoursItem.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)
        viewBinding.tvHoursSymbol.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)
        viewBinding.tvMinutesItem.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)
        viewBinding.tvMinutesSymbol.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)
        viewBinding.tvSecondsItem.setPadding(downPaddingLeft, downPaddingTop, downPaddingRight, downPaddingBottom)


        if (downPadding != 0) {
            viewBinding.tvDayItem.setPadding(downPadding, downPadding, downPadding, downPadding)
            viewBinding.tvDaySymbol.setPadding(downPadding, downPadding, downPadding, downPadding)
            viewBinding.tvHoursItem.setPadding(downPadding, downPadding, downPadding, downPadding)
            viewBinding.tvHoursSymbol.setPadding(downPadding, downPadding, downPadding, downPadding)
            viewBinding.tvMinutesItem.setPadding(downPadding, downPadding, downPadding, downPadding)
            viewBinding.tvMinutesSymbol.setPadding(downPadding, downPadding, downPadding, downPadding)
            viewBinding.tvSecondsItem.setPadding(downPadding, downPadding, downPadding, downPadding)
        }
        viewBinding.tvDayItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)
        viewBinding.tvDaySymbol.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)
        viewBinding.tvHoursItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)
        viewBinding.tvHoursSymbol.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)
        viewBinding.tvMinutesItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)
        viewBinding.tvMinutesSymbol.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)
        viewBinding.tvSecondsItem.setTextSize(TypedValue.COMPLEX_UNIT_PX, downTextSize)

        viewBinding.tvDayItem.setTextColor(downColor)
        viewBinding.tvDaySymbol.setTextColor(downSymbolColor)
        viewBinding.tvHoursItem.setTextColor(downColor)
        viewBinding.tvHoursSymbol.setTextColor(downSymbolColor)
        viewBinding.tvMinutesItem.setTextColor(downColor)
        viewBinding.tvMinutesSymbol.setTextColor(downSymbolColor)
        viewBinding.tvSecondsItem.setTextColor(downColor)

        if (downHeight != -1f) {
            viewBinding.tvDayItem.height = downHeight.toInt()
            viewBinding.tvDaySymbol.height = downHeight.toInt()
            viewBinding.tvHoursItem.height = downHeight.toInt()
            viewBinding.tvHoursSymbol.height = downHeight.toInt()
            viewBinding.tvMinutesItem.height = downHeight.toInt()
            viewBinding.tvMinutesSymbol.height = downHeight.toInt()
            viewBinding.tvSecondsItem.height = downHeight.toInt()
        }
        if (downDrawable != -1) {
            viewBinding.tvDayItem.setBackgroundResource(downDrawable)
            viewBinding.tvDaySymbol.setBackgroundResource(downDrawable)
            viewBinding.tvMinutesItem.setBackgroundResource(downDrawable)
            viewBinding.tvSecondsItem.setBackgroundResource(downDrawable)
        }
    }

    fun start(time: String, pattern: String, onFinish: () -> Unit) {
        val endTime = TimeUtils.string2Millis(time, pattern)
        val currentTimeMillis = System.currentTimeMillis()
        var millisTotal = endTime - currentTimeMillis
        if (downTime != null) {
            return
        }

        downTime = object : CountDownTimer(millisTotal, 1000L) {
            override fun onTick(millisUntilFinished: Long) {
                LogUtils.e(millisUntilFinished)
                val formatTimes = millisUntilFinished.formatTimes()

                if (formatTimes[0] == "00") {
                    viewBinding.tvDayItem.gone()
                    viewBinding.tvDaySymbol.gone()
                }

                viewBinding.tvHoursItem.text = formatTimes[1]
                viewBinding.tvMinutesItem.text = formatTimes[2]
                viewBinding.tvSecondsItem.text = formatTimes[3]
            }

            override fun onFinish() {
                onFinish()
            }
        }
        downTime?.start()
    }

    fun cancel() {
        downTime?.cancel()
        downTime = null
    }
}